//
//  IdeaBackViewController.m
//  quickDoctor
//
//  Created by sdos on 2017/3/10.
//  Copyright © 2017年 SWS. All rights reserved.
//

#import "IdeaBackViewController.h"
#import <Masonry.h>
#import <MBProgressHUD.h>

#define MAX_NUM 200

#define RGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]

// 宏定义 随机颜色
#define RandomColor RGBColor(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255))


@interface IdeaBackViewController ()<UITextViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UITextView *textView;
@property(nonatomic,strong)UIButton *sendButton;
@property(nonatomic,strong)UILabel *countabel; //限制字符数并显示label

@end

static NSString *identifier = @"idea";

@implementation IdeaBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];

}

- (void)setupUI
{
    UITextView *textView = [[UITextView alloc] init];
    textView.textAlignment = NSTextAlignmentJustified;
    self.automaticallyAdjustsScrollViewInsets = NO;
   
    textView.text = @"请输入反馈信息";
    textView.delegate = self;
    textView.textColor = [UIColor grayColor];
    _textView = textView;
    
    textView.backgroundColor = RGBColor(235, 235, 241);
    [self.view addSubview:textView];
    
    
    
    UIButton *sendButton = [[UIButton alloc] init];
    [sendButton setTitle:@"提交反馈" forState:UIControlStateNormal];
    _sendButton = sendButton;
    sendButton.backgroundColor = RGBColor(41, 188, 186);
    sendButton.tintColor = [UIColor whiteColor];
    [self.view addSubview:sendButton];
    //点击
    [sendButton addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *countabel = [[UILabel alloc] init];
    countabel.text = @"       您还可以输入的字数为: 200";
    _countabel = countabel;
    countabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:countabel];
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.text = @"  欢迎联系我们";
    label2.font = [UIFont systemFontOfSize:18];
    label2.backgroundColor = RGBColor(235, 235, 241);
    [self.view addSubview:label2];
    
    self.navigationController.navigationBar.translucent = NO;
    //约束
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(10);
        make.leading.equalTo(self.view).offset(10);
        make.trailing.equalTo(self.view).offset(-10);
        make.height.mas_equalTo(200);
    }];
    [sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(textView);
        make.top.equalTo(textView.mas_bottom).offset(5);
        make.size.mas_equalTo(CGSizeMake(100, 40));
    }];
    [countabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(textView);
        make.top.equalTo(sendButton);
        make.trailing.equalTo(sendButton.mas_leading).offset(10);
        make.height.mas_equalTo(40);
    }];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(countabel.mas_bottom).offset(5);
        make.leading.trailing.offset(0);
        make.height.mas_equalTo(50);
    }];
    
    //添加底部tableView
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView = tableView;
    tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.1)];
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.dataSource = self;
    tableView.delegate = self;
    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:tableView];
    
    //约束
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label2.mas_bottom).offset(0);
        make.leading.trailing.offset(0);
        make.height.mas_equalTo(300);
    }];
    
    //添加手势 收回键盘
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(reKeyBoard)];
//    [self.view addGestureRecognizer:tap];
    
}
#pragma mark - 点击界面 回收键盘
//- (void)reKeyBoard
//{
//    [self.view endEditing:YES];
//}
#pragma mark
#pragma mark - tableView数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    NSInteger row = indexPath.row;
    switch (row) {
        case 0:{
            cell.imageView.image = [UIImage imageNamed:@"dianhua"];
            cell.textLabel.text = @"客服电话: 400-636-6113";
            break;
        }
        case 1:{
            cell.imageView.image = [UIImage imageNamed:@"qq"];
            cell.textLabel.text = @"用户交流群: QQ 464768569";
            break;
        }
        default:
            break;
    }
    return cell;
}


#pragma mark
#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length == 0) {
        textView.text = @"请输入反馈信息";
        textView.textColor = [UIColor grayColor];
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"请输入反馈信息"]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
    }
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSString *str = [textView.text stringByReplacingCharactersInRange:range withString:text];
    NSInteger num =MAX_NUM - str.length;
    if (num>=0) {
        return YES;
    }else
    {
        NSInteger len = text.length + num;
        //防止当text.length + caninputlen < 0时，使得rg.length为一个非法最大正数出错
        NSRange rg = {0,MAX(len,0)};
        
        if (rg.length > 0)
        {
            NSString *s = [text substringWithRange:rg];
            [textView setText:[textView.text stringByReplacingCharactersInRange:range withString:s]];
        }
        return NO;
    }
}
- (void)textViewDidChange:(UITextView *)textView
{
    NSString  *nsTextContent = textView.text;
    NSInteger existTextNum = nsTextContent.length;
    
    if (existTextNum > MAX_NUM)
    {
        //截取到最大位置的字符
        NSString *s = [nsTextContent substringToIndex:MAX_NUM];
        
        [textView setText:s];
    }
    _countabel.text = [NSString stringWithFormat:@"       您还可以输入的字数为: %lu",MAX(0,MAX_NUM - existTextNum)];
}



#pragma mark
#pragma mark - 点击反馈按钮
- (void)clickBtn:(UIButton *)sender{
    if ([_countabel.text isEqualToString:@"       您还可以输入的字数为: 200"]) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.mode = MBProgressHUDModeCustomView;
        UIImage *image = [[UIImage imageNamed:@"compose_photo_close"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        hud.customView = [[UIImageView alloc] initWithImage:image];
        hud.square = YES;
        hud.label.text = NSLocalizedString(@"请输入反馈信息!", @"HUD done title");
        hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.backgroundView.color = [UIColor colorWithWhite:0.1f alpha:0.1f];
        [hud hideAnimated:YES afterDelay:1.f];
    } else
    {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.mode = MBProgressHUDModeCustomView;
        UIImage *image = [[UIImage imageNamed:@"Checkmark"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        hud.customView = [[UIImageView alloc] initWithImage:image];
        hud.square = YES;
        hud.label.text = NSLocalizedString(@"谢谢您的意见反馈", @"HUD done title");
        hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.backgroundView.color = [UIColor colorWithWhite:0.1f alpha:0.1f];
        [hud hideAnimated:YES afterDelay:1.f];
    }
    [self.textView endEditing:YES];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        //打电话
        NSURL *url = [NSURL URLWithString:@"tel://10010"];
        [[UIApplication sharedApplication]openURL:url];
        NSLog(@"打电话");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"是否拨打电话: 400-636-6113" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
